<?php

namespace app\admin\controller;

use think\Controller;
use think\Request;
use think\Cache;

class Index extends Base
{
    /**
     * 显示资源列表
     *
     * @return \think\Response
     */
    public function index()
    {
        $info = array(
            '操作系统' => PHP_OS,
            '运行环境' => $_SERVER["SERVER_SOFTWARE"],
            'PHP版本'   => PHP_VERSION,
            '主机名' => $_SERVER['SERVER_NAME'],
            'WEB服务端口' => $_SERVER['SERVER_PORT'],
            '网站文档目录' => $_SERVER["DOCUMENT_ROOT"],
            '设备信息' => $this->getOS(),
            '浏览器信息' => $this->getBrowser(),
            '通信协议' => $_SERVER['SERVER_PROTOCOL'],
            '上传附件限制' => ini_get('upload_max_filesize'),
            '执行时间限制' => ini_get('max_execution_time') . '秒',
            //'服务器时间' => date("Y年n月j日 H:i:s"),
            //'北京时间' => gmdate("Y年n月j日 H:i:s", time() + 8 * 3600),
            '服务器域名/IP' => $_SERVER['SERVER_NAME'] . ' [ ' . gethostbyname($_SERVER['SERVER_NAME']) . ' ]',
            '用户的IP地址' => $_SERVER['REMOTE_ADDR'],
            '剩余空间' => round((disk_free_space(".") / (1024 * 1024)), 2) . 'M',
            'ThinkPHP版本' => THINK_VERSION,
        );
        $this->assign('info', $info);
        return view();
    }

    /**
     * 获取浏览器信息
     */
    public static function getBrowser()
    {
        $agent = $_SERVER["HTTP_USER_AGENT"];
        if (strpos($agent, 'MSIE') !== false || strpos($agent, 'rv:11.0')) //ie11判断
        {
            return "Internet Explorer";
        } elseif (strpos($agent, 'Edge') !== false) {
            return "Microsoft Edge";
        } elseif (strpos($agent, 'Firefox') !== false) {
            return "FireFox";
        } elseif (strpos($agent, 'Chrome') !== false) {
            return "Chrome";
        } elseif (strpos($agent, 'Opera') !== false) {
            return 'Opera';
        } elseif ((strpos($agent, 'Chrome') == false) && strpos($agent, 'Safari') !== false) {
            return 'Safari';
        } elseif (strpos($agent, 'MicroMessenger') !== false) {
            return 'WeiXin';
        } else {
            return 'unknown';
        }

    }

    function getOS()
    {
        $agent = strtolower($_SERVER['HTTP_USER_AGENT']);
        if (strpos($agent, 'windows nt')) {
            $platform = 'Windows';
        } elseif (strpos($agent, 'macintosh')) {
            $platform = 'Mac';
        } elseif (strpos($agent, 'ipod')) {
            $platform = 'iPod';
        } elseif (strpos($agent, 'ipad')) {
            $platform = 'iPad';
        } elseif (strpos($agent, 'iphone')) {
            $platform = 'iPhone';
        } elseif (strpos($agent, 'android')) {
            $platform = 'Android';
        } elseif (strpos($agent, 'unix')) {
            $platform = 'Unix';
        } elseif (strpos($agent, 'linux')) {
            $platform = 'Linux';
        } else {
            $platform = 'Other';
        }
        return $platform;
    }

//获取ip
    function getIP()
    {
        global $_SERVER;
        if (getenv('HTTP_CLIENT_IP')) {
            $ip = getenv('HTTP_CLIENT_IP');
        } else if (getenv('HTTP_X_FORWARDED_FOR')) {
            $ip = getenv('HTTP_X_FORWARDED_FOR');
        } else if (getenv('REMOTE_ADDR')) {
            $ip = getenv('REMOTE_ADDR');
        } else {
            $ip = $_SERVER['REMOTE_ADDR'];
        }
        return $ip;
    }

    /**
     * 清空缓存
     *
     * @return \think\Response
     */
    public function delCache()
    {
        $res = Cache::clear();
        if (!$res){
            $this->error('清空缓存失败');
        }
        $this->success('清空缓存成功','Index/index');
    }
}
